package com.example.studentmanager;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class UpdateStudent extends AppCompatActivity {
    EditText xuehao, gender, adress, phone, chenji;
    Button btnupdate;
    StudentDB studentDB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_student);
        xuehao = findViewById(R.id.xuehao);
        gender = findViewById(R.id.gender);
        adress = findViewById(R.id.adress);
        phone = findViewById(R.id.phone);
        chenji = findViewById(R.id.chengji);
        btnupdate = findViewById(R.id.btnupdate);
        studentDB = StudentDB.getStudentDB(UpdateStudent.this);

        Intent intent = getIntent();
        String xx = intent.getStringExtra("xx").substring(4);
        xuehao.setText(xx);


        btnupdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (xuehao.getText().toString().isEmpty() ||
                        gender.getText().toString().isEmpty() ||
                        adress.getText().toString().isEmpty() ||
                        phone.getText().toString().isEmpty() ||
                        chenji.getText().toString().isEmpty()) {
                    Toast.makeText(UpdateStudent.this, "输入的信息有空！", Toast.LENGTH_SHORT).show();

                }
                String xuehaos = xuehao.getText().toString();
                String genders = gender.getText().toString();
                String adresses = adress.getText().toString();
                String phones = phone.getText().toString();
                String chenjis = chenji.getText().toString();
                boolean is = studentDB.updateById(xuehaos, genders, adresses, phones, chenjis);
                if (is) {
                    Toast.makeText(UpdateStudent.this, "更新成功", Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(UpdateStudent.this, "更新失败,学号有误，无此学生！", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.v("update", "onResume");

    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.v("update", "onPause");

    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.v("update", "onStop");
    }
}